package com.shouer.doc.widget;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * Created by yangyayun on 2016/12/22.
 */

public class AminalImageView extends ImageView {

    private AnimationDrawable anim;

    public AminalImageView(Context context) {
        super(context);
        anim = (AnimationDrawable) this.getDrawable();
    }

    public AminalImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        anim = (AnimationDrawable) this.getDrawable();

    }

    public AminalImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        anim = (AnimationDrawable) this.getDrawable();
    }

    public void setImageResource(int resId) {
        super.setImageResource(resId);
        if (null != anim) {
            if (anim.isRunning())
                anim.stop();
            anim = (AnimationDrawable) this.getDrawable();
            anim.start();
        } else {
            anim = (AnimationDrawable) this.getDrawable();
            anim.start();
        }

    }

    public void setImageDrawable(AnimationDrawable animationDrawable) {
        super.setImageDrawable(animationDrawable);
        if (null != anim) {
            if (anim.isRunning())
                anim.stop();
            anim = (AnimationDrawable) this.getDrawable();
            anim.setOneShot(false);
            anim.start();
        } else {
            anim = (AnimationDrawable) this.getDrawable();
            anim.setOneShot(false);
            anim.start();
        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);

    }

    public AnimationDrawable getAnim() {
        return anim;
    }

    public void setAnim(AnimationDrawable anim) {
        this.anim = anim;
    }
}